/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.controller.service;

import akka.actor.ActorRef;
import com.cxhd.pacs.actor.ActorManager;
import com.cxhd.pacs.model.Asklist;
import com.cxhd.pacs.tools.DateUtil;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author 52461
 */
public class AskService {

    public String getAskCode() {

        int count = Integer.parseInt(Db.findFirst("select count(*) as count from asklist where to_days(createtime)=to_days(now())").get("count") + "") + 1;
        String num = DateUtil.getDateNum() + count + "";
        if (num.length() == 1) {
            num = "000" + num;
        } else if (num.length() == 2) {
            num = "00" + num;
        } else if (num.length() == 3) {
            num = "0" + num;
        }
        return num;

    }

    public Map<String, String> createAsk(Map<String, String> map, Controller con) {
        String ask_code = getAskCode();
        String ask_pk = null;
        Asklist rd = new Asklist();
        rd.set("ask_code", ask_code);
        rd.set("patient_id", map.get("host_id"));
        rd.set("patient_name", map.get("name"));
        rd.set("phone", map.get("phone"));
        rd.set("sex", map.get("sex"));
        rd.set("age", map.get("age"));
        rd.set("sender_id", map.get("sender_id"));
        rd.set("sender_name", map.get("sender_name"));
        rd.set("sender_phone", map.get("sender_phone"));
        rd.set("apply_department", map.get("apply_department"));
        rd.set("description", map.get("mudi"));
        rd.set("hospital_name", map.get("hospital"));
        rd.set("createtime", DateUtil.getDateEN());
        if (map.get("ask_pk") == null) {

            rd.save();
            ask_pk = rd.get("pk") + "";
            map.put("ask_pk", ask_pk);
            sendSms(map);
            saveHostDetail(map, ask_code);
        } else {
            rd.findById(map.get("ask_pk"))
                    .set("patient_id", map.get("host_id"))
                    .set("patient_name", map.get("name"))
                    .set("phone", map.get("phone"))
                    .set("sex", map.get("sex"))
                    .set("age", map.get("age"))
                    .set("sender_id", map.get("sender_id"))
                    .set("sender_name", map.get("sender_name"))
                    .set("sender_phone", map.get("sender_phone"))
                    .set("apply_department", map.get("apply_department"))
                    .set("description", map.get("mudi"))
                    .set("hospital_name", map.get("hospital"))
                    .update();
            ask_code = map.get("ask_code");
            ask_pk = map.get("ask_pk");
        }
        saveAskDetail(ask_code, con);
        map.put("ask_pk", ask_pk);

        Map<String, String> result = new HashMap<String, String>();
        result.put("ask_pk", ask_pk);
        result.put("ask_code", ask_code);
        return result;
    }

    public void saveAskDetail(String ask_code, Controller con) {

        Db.update("replace into ask_detail set ask_code='" + ask_code + "',datail='" + con.getPara("data") + "' ");
    }

    public void sendSms(Map<String, String> map) {

        ActorManager.getIntance().getActor("sendSmsActor").tell(map, ActorRef.noSender());
    }

    public void saveHostDetail(Map<String, String> map, String ask_code) {
        Record map_detail = new Record();
        map_detail.set("host_id", map.get("host_id"));
        map_detail.set("event_name", "会诊申请单");
        map_detail.set("department", map.get("apply_department"));
        //map_detail.set("event_description", map.get("mudi"));
        map_detail.set("event_result", ask_code);
        map_detail.set("event_time", DateUtil.getDateEN());
        map_detail.set("result_type", "apply");
        map_detail.set("update_id", ask_code);
        Db.save("host_detail", map_detail);
    }

    public  synchronized String updateAskRecvier(String recvicer_id, String recvicer_phone, String recvicer_name, String appointment_time, String contact_id, String contact_phone, String contact_name, String ask_code) {
        String data = "true";
        Record re = Db.findFirst("select * from asklist where recvicer_id is not null and ask_code='" + ask_code + "' ");
        if (re != null && re.get("recvicer_id") != null) {
            data = "已经被 【" + re.getStr("contact_name") + "】处理";
        } else {
            Db.update("update asklist set recvicer_id=?,recvicer_phone=?,recvicer_name=?,appointment_time=?,contact_id=?,contact_phone=?,contact_name=? where ask_code=? ",
                    recvicer_id, recvicer_phone, recvicer_name, appointment_time, contact_id, contact_phone, contact_name, ask_code);
            //getPara("recvicer_id"), getPara("recvicer_phone"), getPara("recvicer_name"), getPara("appointment_time"), loginUserInfo.get("username") + "", loginUserInfo.get("phone") + "", loginUserInfo.get("ch_name") + "", getPara("ask_code"));

        }
        return data;

    }
}
